package vendingMachine.finance;

public class AuthorizeThread extends Thread
{
	private AuthorizeCallback callback;
	private PaymentType type;
	private float total;
	private int id;
	private AuthorizerCollection collection;
	private AuthorizeObject obj;

	public AuthorizeThread(int id, AuthorizeCallback cb, PaymentType type,
						   float total,AuthorizerCollection collection,
						   AuthorizeObject obj)
	{
		this.id = id;
		this.callback = cb;
		this.type = type;
		this.total = total;
		this.collection = collection;
		this.obj = obj;
		this.obj.setSessionId(this.id);
	}

	public void run()
	{
		DataAuthorizor authorizor = collection.getAuthorizerClass(type.getType());
		System.out.println("Got Authorize Class");
		if(authorizor != null)
		{
			System.out.println("Getting Authorization");
			AuthorizeObject o = authorizor.getAuthorization(type,total,obj);
			synchronized(this)
			{
				System.out.println("Sending to Callback");
				callback.onAuthorize(o);
			}
		}
		else
		{
			synchronized(this)
			{
				obj.setAuthorize(false);
				callback.onAuthorize(obj);
			}
		}
	}
}
